package com.handz.test.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;

/**
 * 应用模块名称
 * <p>
 *
 * @author hepeiyun
 * @since 2019/11/5
 */
@Slf4j
public class DemoFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        log.info("doFilter,url:{}", request.getRequestURL());
        if (request.getRequestURL().indexOf("demo1") != -1 || request.getRequestURL().indexOf("demo2") != -1 || request.getRequestURL().indexOf("demo3") != -1) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse);
            wrapper.sendRedirect("/demo/demo2");
        }
    }

}
